<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form size="large" class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入设备名称" clearable @input="handleQuery"
          @keyup.enter="handleQuery" class="!w-240px" />
      </el-form-item>
      <el-form-item label="设备分类" prop="equipmentClassify">
        <el-select filterable @change="handleQuery" clearable class="!w-240px" v-model="queryParams.equipmentClassify"
          placeholder="请选择设备分类">
          <el-option v-for="dict in equipmentClassify" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table row-key="id" border v-loading="loading" :cell-style="planDateCellStyle" :data="list"
      :show-overflow-tooltip="true" @selection-change="handleRowCheckboxChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="设备名称" align="center" prop="equipmentName" width="120px">
        <template #default="{ row }">
          <el-button type="success" text style="font-size: 14px;" @click="openViewDialog(row.id)">
            {{ row.equipmentName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="设备编码" align="center" prop="code" width="120px" />
      <el-table-column label="所属部门" align="center" prop="deptName" width="120px" />
      <el-table-column label="设备分类" align="center" prop="equipmentClassifyName" width="120px" />
      <el-table-column label="规格型号" align="center" prop="equipmentModel" width="120px" />
      <el-table-column label="安装位置" align="center" prop="installPosition" width="120px" />
      <el-table-column label="上次点检时间" align="center" width="220" prop="lastCheckTime" :formatter="dateFormatter" />
      <el-table-column label="下次点检时间" align="center" width="220" prop="nextCheckTime" :formatter="dateFormatter" />
      <el-table-column label="操作" align="center" min-width="150px" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('create', scope.row.id)">
            点检
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <!-- <EquipmentManageForm ref="formRef" @success="getList" /> -->
  <EquipmentCheckRecordForm ref="formRef" @success="getList" />

  <!-- 详细信息 -->
  <EquipmentInfoView ref="viewRef" />

</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'
import { isEmpty } from '@/utils/is'
import download from '@/utils/download'
import { EquipmentManageApi, EquipmentManage } from '@/api/business/equipmentmanage'
import EquipmentManageForm from './EquipmentManageForm.vue'
import { EquipmentTypeApi } from '@/api/business/equipmenttype'
import EquipmentInfoView from './EquipmentInfoView.vue'
import EquipmentCheckRecordForm from './EquipmentCheckRecordForm.vue'
import { EquipmentCheckTypeApi, EquipmentCheckType } from '@/api/business/equipmentchecktype'

/** 设备台账 列表 */
defineOptions({ name: 'EquipmentManage' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<EquipmentManage[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  equipmentName: undefined,
  deptName: undefined,
  equipmentClassify: undefined,
  equipmentStatus: undefined,
  equipmentType: undefined,
  installPosition: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const equipmentClassify = ref([]) // 树形结构

const planDateCellStyle = ({ row, column, rowIndex, columnIndex }) => {
  if (column.property === 'lastCheckTime' || column.property === "nextCheckTime") {
    return {
      backgroundColor: row.color || 'transparent',
      textAlign: 'center'
    };
  }
  // 其他列返回默认样式
  return {};
};

//获取所有分类
const getTypeList = async () => {
  const res = await EquipmentTypeApi.getEquipmentTypeList();
  equipmentClassify.value = res
}


const rowStyle = ({ row }) => {
  const bgColor = row.color || ''
  console.log("??????", bgColor);

  return {
    backgroundColor: bgColor,

  }
}


/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EquipmentManageApi.getEquipmentManageCheckPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}


/** 添加/修改操作 */
const formRef = ref()
const openForm = async (type: string, id?: number) => {
  const params = {
    targetId: id,
    type: 2//按设备
  }
  //判断有没有维护点检项
  const res = await EquipmentCheckTypeApi.checkTypeMapList(params)
  console.log(".....", res);


  if (res.bind == null || res.bind == undefined || res.bind.length == 0) {
    message.error("请先在设备点检配置中维护点检项")
    return
  }


  formRef.value.open(type, id)
}



const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: EquipmentManage[]) => {
  checkedIds.value = records.map((item) => item.id);
}


const viewRef = ref()

const openViewDialog = (id) => {
  viewRef.value.openDetailInfo(id)
}



/** 初始化 **/
onMounted(() => {
  getList()
  getTypeList()
})
</script>
